﻿@using OnWork.DataCenter.BLL.Standard;
@using OnWork.DataCenter.BLL.DataCenter;
@using OnWork.DataCenter.DAL.DataCenter;
@using OnWork.DataCenter.Utils.Caches;
@using SqlSugar;
@using Mapster;
@inherits ComponentBaseDataCenter;
<ValidateForm Model="@config"
              OnValidSubmit="@OnValidSubmit">
    <div class="row g-3 form-inline">
        <div class="col-12 col-sm-6">
            <Select PlaceHolder="请选择数据连接"
                    TValue="long"
                    IsSelectAllTextOnEnter="true" Items="@Conns" ShowSearch="true"
                    ShowLabel="true"
                    DisplayText="数据连接" @bind-Value="@config.DBID" OnSelectedItemChanged="OnSelectedItemChangedAsync" />
        </div>
        <div class="col-sm-12">
           <Textarea placeholder="请输入SQL语句 ..." rows="16" @bind-Value="@config.SQL"></Textarea>
        </div>
    </div>
    <div class="form-footer mt-3">
        <DialogCloseButton />
        <Button Color="Color.Primary" ButtonType="ButtonType.Submit" Icon="fa-solid fa-floppy-disk" Text="保存" />
    </div>
</ValidateForm>


@code {

    [CascadingParameter(Name = "BodyContext")]
    private object? Context { get; set; }

    [CascadingParameter]
    private Modal? Dialog { get; set; }

    [Parameter]
    public Action<DBToDataTableConfig>? OnSave { get; set; }

    private DBToDataTableConfig config;


    /// <summary>
    /// 依赖注入 数据库配置表
    /// </summary>
    [Inject]
    protected DSDataBaseBLL dsDataBaseBLL { get; set; }

    protected override void OnParametersSet()
    {
        base.OnParametersSet();

        if (Context is OnWork.DataCenter.BLL.Standard.DBToDataTableConfig)
        {
            config = Context as OnWork.DataCenter.BLL.Standard.DBToDataTableConfig;
        }
    }


    /// <summary>
    /// 数据库连接列表
    /// </summary>
    private List<SelectedItem> Conns;

    /// <summary>
    /// 数据库连接
    /// </summary>
    private List<DSDataBase> dSDataBases;


    protected override void OnInitialized()
    {
        base.OnInitialized();
        //查询列表
        if (Conns is null)
        {
            dSDataBases = dsDataBaseBLL.GetALL();
            Conns = dSDataBases.Select(x => new SelectedItem() { Text = x.Name, Value = x.ID.ToString() }).ToList();
        }
    }


    /// <summary>
    /// 当选择项目修改时
    /// </summary>
    /// <param name="item"></param>
    /// <returns></returns>
    private async Task OnSelectedItemChangedAsync(SelectedItem item)
    {
        try
        {
            var obj = dSDataBases.Where(x => x.ID == long.Parse(item.Value)).FirstOrDefault();
            if (obj != null)
            {
                config.DBConn = obj.ConnStr; //连接字符串
                config.DBType = (DbType)obj.Type; //数据库类型
            }
        }
        catch (Exception ex)
        {
            await base.MessageError(ex.ToString());
        }
    }

    private async Task OnValidSubmit(EditContext item)
    {
        // do someting like save model into db
        OnSave(config);

        await Dialog.Close();
    }
}
